package com.janetfilter.core;

import java.io.File;
import java.lang.instrument.Instrumentation;
import java.net.URI;
import java.util.jar.JarFile;

import com.janetfilter.core.commons.DebugInfo;
import com.janetfilter.core.utils.WhereIsUtils;

public class Launcher {
	public static final String VERSION = "2022.2.0";
	public static final int VERSION_NUMBER = 202201000;

	private static boolean loaded = false;

	public static void main(String[] args) {

		try {
			WhereIsUtils.getJarURI();
		} catch (Throwable e) {
			DebugInfo.error("Can not locate `ja-netfilter` jar file.", e);
			return;
		}

		printUsage();

	}

	public static void premain(String args, Instrumentation inst) {
		premain(args, inst, false);
	}

	public static void agentmain(String args, Instrumentation inst) {
		if (null == System.getProperty("janf.debug")) {
			System.setProperty("janf.debug", "1");
		}

		if (null == System.getProperty("janf.output")) {
			System.setProperty("janf.output", "3");
		}

		premain(args, inst, true);
	}

	private static void premain(String args, Instrumentation inst, boolean attachMode) {
		if (loaded) {
			DebugInfo.warn("You have multiple `ja-netfilter` as javaagent.");
			return;
		}

		printUsage();

		URI jarURI;
		try {
			loaded = true;
			jarURI = WhereIsUtils.getJarURI();
		} catch (Throwable e) {
			DebugInfo.error("Can not locate `ja-netfilter` jar file.", e);
			return;
		}

		File agentFile = new File(jarURI.getPath());
		try {
			inst.appendToBootstrapClassLoaderSearch(new JarFile(agentFile));
		} catch (Throwable e) {
			DebugInfo.error("Can not access `ja-netfilter` jar file.", e);
			return;
		}

		Initializer.init(new Environment(inst, agentFile, args, attachMode)); // for some custom UrlLoaders
	}

	private static void printUsage() {
		String content = "\n  ============================================================================  \n" + "\n"
				+ "    ja-netfilter " + VERSION + "\n\n" + "    A javaagent framework :)\n" + "\n"
				+ "    https://gitee.com/ja-netfilter/ja-netfilter\n" + "\n"
				+ "  ============================================================================  \n\n";

		System.out.print(content);
	}
}
